<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/11/27
 * Time: 14:01
 */
namespace app\admin\controller;
use think\Db;
class Money extends Base {

    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $where2 = [];
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $where2['revenue_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $where2['revenue_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $where2['revenue_time'] = ['between time',[$start,$end]];
            }
            if(isset($status)&&$status!="")
            {
                $where2['revenue_type'] = $status;
            }
            if(isset($number)&&$number!="")
            {
                $where2['revenue_number|u1.user_phone|u1.user_username'] = ['like',"%$number%"];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $field = 'l.*,u1.user_username name, u1.user_phone phone,u2.user_username name2, u2.user_phone phone2,h_content';
            $count = Db::table('up_revenue')->alias('l')
                ->join('up_u_user u1', 'l.revenue_user_id = u1.user_id','left')
                ->join('up_u_user u2', 'l.revenue_other_id = u2.user_id','left')
                ->join('up_hot h', 'l.revenue_other_id = h.h_id','left')
                ->where($where2)
                ->count();// 获取总条数
            $lists = Db::table('up_revenue')->alias('l')
                ->join('up_u_user u1', 'l.revenue_user_id = u1.user_id','left')
                ->join('up_u_user u2', 'l.revenue_other_id = u2.user_id','left')
                ->join('up_hot h', 'l.revenue_other_id = h.h_id','left')
                ->where($where2)
                ->page($Nowpage,$limits)
                ->order('revenue_time desc')
                ->field($field)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 导出Excel
     * @return \think\response\Json
     */
    public function excelwater(){
        extract(input());
        $where2 = [
            'revenue_id' => ['>',0]
        ];
        if(isset($start)&&$start!=""&&isset($end)&&$end=="")
        {
            $where2['revenue_time'] = ['>= time',$start];
        }
        if(isset($end)&&$end!=""&&isset($start)&&$start=="")
        {
            $where2['revenue_time'] = ['<= time',$end];
        }
        if(isset($start)&&$start!=""&&isset($end)&&$end!="")
        {
            $where2['revenue_time'] = ['between time',[$start,$end]];
        }
        if(isset($status)&&$status!="")
        {
            $where2['revenue_type'] = $status;
        }
        if(isset($number)&&$number!="")
        {
            $where2['revenue_number|user_phone'] = ['like',"%$number%"];
        }
        if(isset($one_type)&&$one_type!="")
        {
            $where2['revenue_one_type'] = $one_type;
        }
        $data = Db::table('up_revenue')->alias('l')
            ->join('up_u_user s', 'l.revenue_user_id = s.user_id','left')
            ->join('up_self_order o', 'l.revenue_order_id = o.order_id','left')
            ->where($where2)
            ->order('revenue_time desc')
            ->field('revenue_number,revenue_money,revenue_type,revenue_one_type,o.pay_type,user_username,user_phone,revenue_time')
            ->select();
        if($data){
            $cellname = [
                '流水单号', '金额', '类型', '订单类型', '支付方式', '用户名称', '用户电话', '时间'
            ];
            $status = ['revenue_type','pay_type','revenue_one_type'];
            $values = [
                'revenue_type' => [1 => '收入',2 => '退款']
                ,'pay_type' => [1 => '微信',2 => '支付宝',3 => '银联']
                ,'revenue_one_type' => [1 => '自营订单',2 => '增补订单',3 => '统购订单',4 => '取消订单']
            ];
            $res = exportExcel('money',time(),$cellname,$data,$status,$values);
            return json($res);
        }else{
            return json(['code'=>0,'msg'=>'此条件下无数据']);
        }
    }
}